package Atm;/*


1111



*/

import java.util.Scanner;

    public class ATMObject {

        private User[] users = {//类型随便放

                new User("longlong", "111111", 1000.0),
                new User("longlong2", "222222", 2000.0),
                new User("longlong3", "333333", 3000.0),
                new User("longlong4", "444444", 4000.0),
                new User("longlong5", "555555", 5000.0),

        };
        private Scanner input = new Scanner(System.in);
        private int index;

        // 控制ATM流程的方法
        public void process() {
            System.out.println("欢迎");
            while (true) {
                login();
                menu();
            }
        }

        // 登录方法
        public void login() {
            for (int i = 0; i < 3; i++) {
                System.out.println("账号：");
                String acount = input.next();
                System.out.println("密码");
                String password = input.next();
                for (int j = 0; j < users.length; j++) {
//                    User u = users[j];
                    if (acount.equals(users[j].getAccount()) && password.equals(users[j].getPassword())) {
                        System.out.println("登陆成功");
                        index = j;
                        return;
                    }
                }
                System.out.println("登录失败，还有" + (2 - i) + "次");
            }
            System.out.println("机会用完");
        }

        //         菜单界面
        public void menu() {
            while (true) {//死循环
                System.out.println("1,存款  2,取款 3,查询  4,转账  5,更换账号  6,退出 7,修改密码");
                int a = input.nextInt();
                if (a == 1) {
                    save();

                } else if (a == 2) {
                    get();


                } else if (a == 3) {
                    search();

                } else if (a == 4) {
                    transfer();


                } else if (a == 5) {
                    System.out.println("退出登录");
                    return;


                } else if (a == 6) {
                    System.out.println("退出程序");
                    System.exit(0);
                } else if (a == 7) {
                    if (changePwd()) {

                        System.out.println("登出");
                        return;
                    }

                } else {
                    System.out.println("输入错误");
                }
            }


        }

        // 存款
        public void save() {
            System.out.println("请输入存款金额：");
            double money = input.nextDouble();
            users[index].setMoney(money+users[index].getMoney());
            System.out.println("存入后金额为" + users[index].getMoney());

        }

        // 取款
        public void get() {

            System.out.println("请输入取款金额");
            double getmony = input.nextDouble();
            if (getmony > users[index].getMoney()) {

                System.out.println("抱歉，你钱不够");

            } else {
                users[index].setMoney(users[index].getMoney()-getmony);
                System.out.println("剩余余额" + users[index].getMoney());
            }
        }

        // 查询
        public void search() {

            System.out.println("余额" + users[index].getMoney());

        }

        // 转账
        public void transfer() {

            System.out.println("请输入转账金额");
            double trunmony = input.nextDouble();
            if (trunmony >users[index].getMoney()) {
                System.out.println("抱歉，余额不足");
                return;
            }

            System.out.println("输入转账用户");
            String duiuser = input.next();
            if (duiuser.equals(users[index])) {
                System.out.println("不可以转个自己：");
                return;
            }
            //账号是否存在
            for (int i = 0; i < users.length; i++) {
                if (users[i].getAccount().equals(duiuser)) {    //数组是equalS
                    //扣钱
                    users[index].setMoney(users[index].getMoney() - trunmony);
                    //加
                    users[i].setMoney(users[i].getMoney() + trunmony);
                    System.out.println("转账成功");
                    return;
                }
            }
            System.out.println("账号输入错误");
        }

        //修改密码
        public  boolean changePwd() {
            System.out.println("请输入原密码：");
            String oldPwd = input.next();
            if (!oldPwd.equals(users[index].getPassword())) {
                System.out.println("原密码输入错误");
                return false;
            }
            System.out.println("请输入新密码：");
            String newPwd = input.next();
            if (oldPwd.equals(newPwd)) {
                System.out.println("新密码不能和原密码一样");
                return false;
            }
            System.out.println("请再次输入新密码：");
            String confirmPwd = input.next();
            if (!newPwd.equals(confirmPwd)) {
                System.out.println("两次密码不一致");
                return false;
            }
            users[index].setPassword(newPwd);
            System.out.println("密码修改成功");
            return true;
        }

        public static void main(String[] args) {
            ATMObject atmObject=new ATMObject();
            atmObject.process();
        }
    }





